package com.sangu.dianxiu.ui;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.VolleyError;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.sangu.dianxiu.BaseApp;
import com.sangu.dianxiu.ICallBack;
import com.sangu.dianxiu.R;
import com.sangu.dianxiu.bean.basedata.CityEntity;
import com.sangu.dianxiu.bean.basedata.CountyEntity;
import com.sangu.dianxiu.bean.user.LoginEntity;
import com.sangu.dianxiu.bean.user.User;
import com.sangu.dianxiu.config.Constants;
import com.sangu.dianxiu.fragment.HomeFragment;
import com.sangu.dianxiu.fragment.MineFragment;
import com.sangu.dianxiu.fragment.OrderFragment;
import com.sangu.dianxiu.fragment.ShoppingFragment;
import com.sangu.dianxiu.utils.AppManager;
import com.sangu.dianxiu.utils.BaseDataManager;
import com.sangu.dianxiu.utils.CheckUtils;
import com.sangu.dianxiu.utils.FragmentTabUtils;
import com.sangu.dianxiu.utils.UserManager;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends BaseActivity implements FragmentTabUtils.OnRgsExtraCheckedChangedListener{

    private UserManager userManager=new UserManager();
    private BaseDataManager baseDataManager=new BaseDataManager();
    private long time;
    private ArrayList<Fragment> fragments=new ArrayList<>();
    @ViewInject(R.id.activity_main_footer_rgs)  //使用XUtils进行注解，这样就不用findViewById了
    private RadioGroup radioGroup;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
//        //透明状态栏
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//        //透明导航栏
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        Fresco.initialize(this);
        ViewUtils.inject(this);     //调用此方法完成注解
        initView();
        //刚进入主界面的时候加载基础数据
        loadBaseData();
    }

    private void initView() {
        fragments.add(new HomeFragment());
        fragments.add(new ShoppingFragment());
        fragments.add(new OrderFragment());
        fragments.add(new MineFragment());
        new FragmentTabUtils(getSupportFragmentManager(),fragments,R.id.activity_main_fragment_container,radioGroup,this);

    }
    private void loadBaseData() {

        if(!CheckUtils.isFirstRun(BaseApp.getInstance()))
        {
            SharedPreferences preferences=getSharedPreferences(Constants.IS_FIRST_RUN, Context.MODE_PRIVATE);
            String phoneNumber=preferences.getString(Constants.PHONE_NUMBER, "");
            String password=preferences.getString(Constants.PASSWORD,"");
            int userId=preferences.getInt(Constants.USER_ID,0);
            BaseApp.user=new User();
            BaseApp.user.setUserPhone(phoneNumber);
            BaseApp.user.setUserId(userId);
            HashMap<String,String> map=new HashMap<>();
            map.put("phone",phoneNumber);
            map.put("pwd",password);
            userManager.login(map, new ICallBack() {
                @Override
                public void onSuccess(String string) {
                    LoginEntity entity=JSONObject.parseObject(string,LoginEntity.class);
                    if(entity.getRc()==0)
                    {
                        BaseApp.isLogin=true;
                    }
                }

                @Override
                public void onError(VolleyError error) {

                }
            });
        }
        HashMap<String,String> cityMap=new HashMap<>();
        baseDataManager.getCity(cityMap, new ICallBack() {
            @Override
            public void onSuccess(String string) {
                CityEntity entity= JSONObject.parseObject(string,CityEntity.class);
                if(entity.getRc()==0)
                {
                    BaseApp.citiesEntity=entity.getCities();
                }
            }

            @Override
            public void onError(VolleyError error) {

            }
        });
        HashMap<String,String> countyMap=new HashMap<>();
        baseDataManager.getCounty(countyMap, new ICallBack() {
            @Override
            public void onSuccess(String string) {
                CountyEntity entity= JSONObject.parseObject(string,CountyEntity.class);
                if(entity.getRc()==0)
                {
                    BaseApp.countiesEntity=entity.getCounties();
                }
            }

            @Override
            public void onError(VolleyError error) {

            }
        });

    }
    //当按返回键的时候提醒用户
    @Override
    public void onBackPressed() {
        long currentTime=System.currentTimeMillis();
        if(currentTime-time>1000)
        {
            time=currentTime;
            BaseApp.getInstance().showToast(getString(R.string.app_exit_msg));
        }
        else
        {   //当按第二次返回键的时候退出程序
            AppManager.getAppManager().AppExit(BaseApp.getInstance());
        }
    }

    @Override
    public void OnRgsExtraCheckedChanged(RadioGroup radioGroup, int checkedId, int index) {

    }

    //在Activity退出的时候取消网络请求
    @Override
    protected void onDestroy() {
        super.onDestroy();
        baseDataManager.requestQueue.cancelAll(BaseDataManager.class);
    }
}
